package cn.dengta.webapp.trade.entity.meta;

import java.math.BigDecimal;

import cn.dengta.webapp.trade.entity.meta.MetaConfig.MetaRuleConfig;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.*;

/**
 * 主播补贴的规则配置对象
 */
@Getter
@Setter
public class AnchorAssessRule implements MetaRuleConfig {

	/** 补贴配置 */
	public AnchorSubsidyEntry[] entries;

	@Override
	public void validate() {
		final AnchorSubsidyEntry[] entries = this.entries;
		Assert.isTrue(X.size(entries) > 0, "补贴奖励配置不能为空！");
		for (int i = entries.length - 1; i > 0; ) {
			AnchorSubsidyEntry entry = entries[i--];
			entry.validate();
			AnchorSubsidyEntry nextEntry = entries[i];
			entry.validate();
			Assert.isTrue(entry.onlineTime >= nextEntry.onlineTime, "在线时长只能递增！");
			Assert.isTrue(entry.greetCount >= nextEntry.greetCount, "搭讪人数只能递增！");
			Assert.isTrue(entry.greetReplyRate.compareTo(nextEntry.greetReplyRate) >= 0, "搭讪回复率只能递增！");
			Assert.isTrue(entry.integralAmount.compareTo(nextEntry.integralAmount) >= 0, "收益金额只能递增！");
			Assert.isTrue(entry.integralAward.compareTo(nextEntry.integralAward) >= 0, "补贴奖励只能递增！");
		}
	}

	@Override
	public void init(MetaRuleConfig config) {
		AnchorAssessRule rule = (AnchorAssessRule) config;
		this.entries = rule.entries;
	}

	@JSONField(serialize = false)
	public AnchorSubsidyEntry getEntry(AnchorSubsidyEntry input, boolean defaultValue) {
		final AnchorSubsidyEntry[] entries = this.entries;
		AnchorSubsidyEntry entry;
		for (int i = entries.length - 1; i >= 0; i--) {
			entry = entries[i];
			if ((entry.onlineTime == 0 || input.onlineTime >= (entry.onlineTime * 60)) &&
					(entry.greetCount == 0 || input.greetCount >= entry.greetCount) &&
					(entry.greetReplyRate.compareTo(BigDecimal.ZERO) == 0 || input.greetReplyRate.compareTo(entry.greetReplyRate) >= 0) &&
					input.integralAmount.compareTo(entry.integralAmount) >= 0) {
				return entry;
			}
		}
		return defaultValue ? entries[0] : null;
	}

	/**
	 * 主播补贴规则实体
	 */
	@Setter
	@Getter
	public static class AnchorSubsidyEntry {

		/** 名称 */
		protected String name;
		/** 在线时长（分）【0代表不限制】 */
		protected Long onlineTime;
		/** 搭讪人数 【0代表不限制】 */
		protected Integer greetCount;
		/** 搭讪回复率% 【0代表不限制】 */
		protected BigDecimal greetReplyRate;
		/** 收益金额（积分） */
		protected BigDecimal integralAmount;
		/** 满足以上条件获得奖励（积分） */
		protected BigDecimal integralAward;

		public AnchorSubsidyEntry() {
		}

		public AnchorSubsidyEntry(String name, Long onlineTime, Integer greetCount, BigDecimal greetReplyRate, BigDecimal integralAmount, BigDecimal integralAward) {
			this.name = name;
			this.onlineTime = onlineTime;
			this.greetCount = greetCount;
			this.greetReplyRate = greetReplyRate;
			this.integralAmount = integralAmount;
			this.integralAward = integralAward;
		}

		public AnchorSubsidyEntry init(String name, Long onlineTime, Integer greetCount, BigDecimal greetReplyRate, BigDecimal integralAmount, BigDecimal integralAward) {
			this.name = name;
			this.onlineTime = X.expectNotNull(onlineTime, 0L);
			this.greetCount = X.expectNotNull(greetCount, 0);
			this.greetReplyRate = X.expectNotNull(greetReplyRate, BigDecimal.ZERO);
			this.integralAmount = X.expectNotNull(integralAmount, BigDecimal.ZERO);
			this.integralAward = X.expectNotNull(integralAward, BigDecimal.ZERO);
			return this;
		}

		public void validate() {
			Assert.isTrue(StringUtil.notEmpty(name), "名称不能为空！");
			Assert.isTrue(onlineTime >= 0, "在线时长不能小于0！");
			Assert.isTrue(greetCount >= 0, "搭讪人数不能小于0！");
			Assert.isTrue(greetReplyRate.compareTo(BigDecimal.ZERO) >= 0, "搭讪回复率不能小于0！");
			Assert.isTrue(integralAmount.compareTo(BigDecimal.ZERO) >= 0, "收益金额不能小于0！");
			Assert.isTrue(integralAward.compareTo(BigDecimal.ZERO) > 0, "补贴奖励必须大于0！");
		}

	}

}
